package com.kfh.ybracelet.find;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.utils.Constant;

/**
 * QA 客服服务页面
 * Created by Aaron on 16/8/19.
 */
public class CustomerServiceActivity extends BaseActivity {

    private WebView wv_about;

    private static String TAG = "CustomerServiceActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_webview);

        initWidget();
    }

    private void initWidget() {

//        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
//        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        View include = findViewById(R.id.include);

        include.setVisibility(View.GONE);

//        tv_title.setText(getResources().getString(R.string.customer_serivce));
//        iv_left.setOnClickListener(this);

        wv_about = (WebView) findViewById(R.id.wv_donate);
        MyWebViewClient client = new MyWebViewClient();

        WebSettings settings = wv_about.getSettings();

        settings.setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        //动态加载js, JsInterface是我们定义的类,用于js调用, android 是动态加载到js代码里(html里面是 javascript:android.closeWindow();)
        //closeWindow 方法是js端调用的本地的方法
        wv_about.addJavascriptInterface(new JsInterface(), "android");

        wv_about.loadUrl(Constant.QA);  // 加载网址
        wv_about.setWebViewClient(client);
        wv_about.setWebChromeClient(new WebChromeClient());

        wv_about.setWebViewClient(new WebViewClient() {

//            @Override
//            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
//                return super.shouldInterceptRequest(view, url);
//            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                return false;
            }
        });

    }

    @SuppressLint("SetJavaScriptEnabled")
    class JsInterface {

        public JsInterface() {
        }

        @JavascriptInterface
        public void closeWindow() {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

//                    showToast(" xxxxxx  JS 调用  xxxxxx");
//                    Log.e(TAG, " 点击 了  xxxx  ");
                    finish();

                }
            });

        }

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void widgetClick(View v) {
//        switch (v.getId()) {
//            case R.id.iv_left:
//                finish();
//                break;
//        }
    }

    @Override
    public void onBackPressed() {

        if (wv_about.canGoBack()) {
            wv_about.goBack();
        } else {
            super.onBackPressed();
        }
    }

}
